#!/usr/bin/python
# -*- coding: UTF-8 -*-


from OpenSSL import crypto
import datetime, os

# 证书路径
cert_file = r'C:\Users\666666\Desktop\chengzijiang.com.pem'
cert = crypto.load_certificate(crypto.FILETYPE_PEM, open(cert_file, 'rb').read())

subject = cert.get_subject()
# 得到证书的域名
issued_to = subject.CN
issuer = cert.get_issuer()
# 得到证书颁发机构
issued_by = issuer.CN
# 获取证书到期时间
notAfter = datetime.datetime.strptime(cert.get_notAfter().decode()[0:-1], '%Y%m%d%H%M%S')
# 获取证书创建时间
notBefor = datetime.datetime.strptime(cert.get_notBefore().decode()[0:-1], '%Y%m%d%H%M%S')

print("本地 {} 证书创建时间: {}".format(cert_file, notBefor))
print("本地 {} 证书到期时间: {}".format(cert_file, notAfter))